/*
 * #%L
 * Client
 * %%
 * Copyright (C) 2011 Ractoc.com
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package com.ractoc.opengamefinder.client.screens.controllers;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.ractoc.opengamefinder.api.messages.ChatLineMessage;
import com.ractoc.opengamefinder.api.results.ChatLineMessageResult;
import com.ractoc.pffj.core.PluginController;
import com.ractoc.pffj.core.PluginException;
import de.lessvoid.nifty.NiftyEventSubscriber;
import de.lessvoid.nifty.controls.ChatTextSendEvent;

/**
 * Main screen controller for the lobby screen.
 * 
 * @author ractoc
 * @version 0.1
 */
public class LobbyScreenController extends BaseScreenController {
    
    private static Logger logger = Logger.getLogger(LobbyScreenController.class.getName());
    
    /**
     * Default constructor.
     */
    public LobbyScreenController() {
    }

    /**
     * Send the text to the server.
     * @param text The text to send.
     */
     @NiftyEventSubscriber(pattern=".*-chat")
    public final void onSendText(final String id, final ChatTextSendEvent event) {
        logger.debug("sending message from " + id + ": " + event.getText());
        if (!StringUtils.isEmpty(event.getText())) {
            final ChatLineMessage clMsg = new ChatLineMessage();
            clMsg.setRoomId(id.split("-")[0]);
            clMsg.setOutBound(true);
            clMsg.setLine(event.getText());
            try {
                final ChatLineMessageResult result = (ChatLineMessageResult) PluginController
                        .getCurrentPluginController().processMessage("chatPlugin", clMsg);
                logger.debug("message result = " + result.getErrorCode());
            } catch (final PluginException e) {
                logger.error("Error processing plugin message.", e);
            }
        }
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    public final void onStartScreen() {
    }

}
